﻿using DrawControl.UI.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
using System.Xml.Linq;

namespace DrawControl.UI.View
{
    public class ExplorerBarGroup : ContainerControl
    {
        private bool _mouseOn = false;// 鼠标是否在
        private bool _expanded = false;// 该组是否展开
        private Rectangle _headerRect;// ExplorerBar的分组标题大小、位置
        private Rectangle _expandedRect;// 展开后的大小
        private Bitmap _headerImage;// 分组的图标
        private Thread _workThread;// 执行展开/收缩的线程
        private Color _bodyColor = SystemColors.InactiveCaption;// 容器背景颜色

        public ExplorerBarGroup(Control owner) : this()
        {
            base.Parent = owner;
        }
        public ExplorerBarGroup() : base()
        {
            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
        }

        public override Rectangle DisplayRectangle
        => new Rectangle(0, _headerRect.Bottom, Width, ClientRectangle.Height - _headerRect.Height);

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            _headerRect = new Rectangle(Point.Empty, GetHeaderSize());
            _headerRect.Width = DisplayRectangle.Width;
            PerformLayout();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (_headerRect.Contains(e.Location))
            {
                Cursor.Current = Cursors.Hand;
                _mouseOn = true;
            }
            else
            {
                Cursor.Current = Cursors.Default;
                _mouseOn = false;
            }
            this.Invalidate(_headerRect);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            PaintHeader(e);
            PaintBody(e);
        }
        protected virtual void PaintHeader(PaintEventArgs e)
        {
            if (VisualStyleRenderer.IsSupported)
            {

            }
        }
        protected virtual void PaintBody(PaintEventArgs e)
        {
            using SolidBrush sb = new SolidBrush(_bodyColor);
            e.Graphics.FillRectangle(sb, _headerRect);
        }
        protected virtual void PaintUnthemedHeader(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Size hsize = GetHeaderSize();
            Color color1 = Color.FromArgb(175, 210, 255);
            Color color2 = ControlPaint.Dark(color1, .1f);
            Size size1 = Size.Round(g.MeasureString(Text, Font));
            Size size2;
            if (size1 == Size.Empty)
                size2 = new Size(Width, Font.Height * 97 / 72 + 6);
            else
                size2 = new Size(Width, size1.Height + 6);
            Rectangle rect = new Rectangle(new Point(0, _headerRect.Height - size2.Height), size2);
            using LinearGradientBrush lbr = new LinearGradientBrush(rect, color1, color2, 90);
            g.FillRectangle(lbr, rect);

            if (_headerImage != null)
            {
                g.DrawImage(_headerImage, new Point(4, (_headerRect.Height - _headerImage.Height) / 2));
            }
            if (!string.IsNullOrEmpty(Text))
            {
                Point p1 = new Point(_headerImage != null ? _headerImage.Width + 8 : 8, rect.Top + (rect.Height - size1.Height) / 2);
                using SolidBrush sb = new SolidBrush(ForeColor);
                g.DrawString(Text, Font, sb, p1);
            }
            Bitmap map = _expanded ? Resources.down_btn_fill : Resources.up_f;
            Point p2 = new Point(rect.Right - map.Width - 4, rect.Top + (rect.Height - map.Height) / 2);
            g.DrawImage(map, p2);
        }
        protected virtual void PaintThemedHeader(PaintEventArgs pa)
        {
            Graphics g = pa.Graphics;
            VisualStyleElement element = VisualStyleElement.ExplorerBar.NormalGroupHead.Normal;
            if (VisualStyleRenderer.IsElementDefined(element))
            {
                VisualStyleRenderer render = new VisualStyleRenderer(element);
                Size size = render.GetPartSize(g, _headerRect, ThemeSizeType.True); size.Width = _headerRect.Width;
                Rectangle rect = new Rectangle(new Point(0, _headerRect.Bottom - size.Height), size);
                render.DrawBackground(g, rect);
                if (_headerImage != null)
                    g.DrawImage(_headerImage, new Point(4, (_headerRect.Height - _headerImage.Height) / 2));
                if (!string.IsNullOrEmpty(Text))
                {
                    Size size1 = Size.Round(g.MeasureString(Text, Font));
                    Point p1 = new Point(_headerImage != null ? _headerImage.Width + 8 : 8, rect.Top + (rect.Height - size1.Height) / 2);
                    Color color1 = render.GetColor(ColorProperty.TextColor);
                    using SolidBrush sb = new SolidBrush(color1);
                    g.DrawString(Text, Font, sb, p1);
                }


                if (!_mouseOn)
                    element = _expanded ? VisualStyleElement.ExplorerBar.NormalGroupExpand.Normal : VisualStyleElement.ExplorerBar.NormalGroupCollapse.Normal;
                else
                    element = _expanded ? VisualStyleElement.ExplorerBar.NormalGroupExpand.Hot : VisualStyleElement.ExplorerBar.NormalGroupCollapse.Hot;

                render = new VisualStyleRenderer(element);
                size = render.GetPartSize(g, ThemeSizeType.True);
                render.DrawBackground(g, new Rectangle(rect.Right - size.Width - 4, rect.Top + (rect.Height - size.Height) / 2, size.Width, size.Height));
            }
            else
                PaintUnthemedHeader(pa);
        }
        private Size GetHeaderSize()
        {
            Size size = Size.Empty;
            if (VisualStyleRenderer.IsSupported)
            {
                VisualStyleElement element = VisualStyleElement.ExplorerBar.NormalGroupExpand.Normal;
                if (VisualStyleRenderer.IsElementDefined(element))
                {
                    VisualStyleRenderer renderer = new VisualStyleRenderer(element);
                    using Graphics g = CreateGraphics();
                    size = renderer.GetPartSize(g, ThemeSizeType.True);
                }
            }

            if (size == Size.Empty)
                size = new Size(Width, Font.Height * 97 / 72 + 6);

            if (_headerImage != null && _headerImage.Height > size.Height)
                size.Height = _headerImage.Height;

            return size;
        }

    }
}
